<?
/**
 * VoiceOne Account
 *
 * Frontend to provide access to the account system in voiceone.
 * @package voiceone
 * @subpackage  account
 */

  include ("../admin/config/cfg.php");
  global $cwlang;
  $alert=getAlert();

  // if user is root die
  if ($cwUserInfo["entity_id"]==0) goToMainPage("","../");

  //================ POST & GET ================
  if(count($_POST)>0) {
    if($cwUserInfo["entity_id"]!=0) {
      $data=$_POST["client"];
      $error="";
      //set user password
      if((isset($data["changepwd"]))&&($data["changepwd"]=="on")){
        if($data["newpassword"]!=""){
          if($data["newpassword"]==$data["confirmpassword"]){
            $newpwd = $data["newpassword"];
            $oldpwd = $data["oldpassword"];
          }else $error=T_("The new password and the confirmation password do not match");
        }else $error=T_("The new password cannot be null");
      }
      if($error==""){
        //set voicemail
        if(isset($data["vemail"])) {
          $vemail=$data["vemail"];
          //if($vemail!=""){
            if((isset($data["vchangepwd"]))&&($data["vchangepwd"]=="on")){
              if($data["vnewpassword"]!=""){
                if($data["vnewpassword"]==$data["vconfirmpassword"]){
                  $vnewpwd = $data["vnewpassword"];
                  $voldpwd = $data["voldpassword"];
                }else $error=T_("The new voiceomail password does not match with the confirmation password");
              }else $error=$T_("The new voiceomail password cannot be null");
            }
          //}else $error=T_("Email address cannot be null");
        }
        if($error==""){
          // put client
          $ret=$asterisk->putClientAccount($data["id"],$data["tech"],$data["language"],$oldpwd,$newpwd,$vemail,$voldpwd,$vnewpwd);
          error_log("return = ".$ret);
          if($ret>0){
            //set asterisk database
            if(isset($_POST["DND"])){ //set DND
              if($_POST["DND"]["status"]==1) $ret=putInDB("DND",$cwUserInfo["exten"],"yes");
              else $ret=rmFromDB("DND",$cwUserInfo["exten"]);
            }
            if(isset($_POST["CFU"])){ //set CFU
              if($_POST["CFU"]["status"]==1) $ret=putInDB("CFU",$cwUserInfo["exten"],$_POST["CFU"]["target"]);
              else $ret=rmFromDB("CFU",$cwUserInfo["exten"]);
            }
            if(isset($_POST["CFB"])){ //set CFB
              if($_POST["CFB"]["status"]==1) $ret=putInDB("CFB",$cwUserInfo["exten"],$_POST["CFB"]["target"]);
              else $ret=rmFromDB("CFB",$cwUserInfo["exten"]);
            }
            if(isset($_POST["CFNR"])){ //set CFNR
              if($_POST["CFNR"]["status"]==1) $ret=putInDB("CFNR",$cwUserInfo["exten"],$_POST["CFNR"]["target"]);
              else $ret=rmFromDB("CFNR",$cwUserInfo["exten"]);
            }
            $alert="<div class=\"AlertGood\">".T_("Extension successfully saved!")."</div>";
            $_GET["id"]=$ret;
          }
          else if($ret==-1) $alert="<div class=\"AlertBad\">".T_("Username and password do not match")."</div>";
          else if($ret==-2) $alert="<div class=\"AlertBad\">".T_("Mailbox and password do not match")."</div>";
          else $alert="<div class=\"AlertBad\">".T_("Some errors occured during saving")."</div>";
        }else $alert="<div class=\"AlertBad\">".$error."</div>";
      }else $alert="<div class=\"AlertBad\">".$error."</div>";
      goToMainPage($alert,"./");
    }
  }
  //================ PAGE LOGIC ================
  //retrive user info
  if ($cwUserInfo["entity_id"]!=0) {
    $ret=$asterisk->getClientAccount($cwUserInfo["entity_id"],0);
    if ($err=$asterisk->getError()) die("Error: $err");
    //extract some variables
    $v=$ret[0];
    $id=$v["id"];
    $name=$v["name"];
    $surname=$v["surname"];
    $alias=$v["alias"];
    $username=$v["extension"];
    $extension=$v["extension"];
    $language=$v["language"];
    $technology=$v["technology"];
    $voicemailArr=$v["voicemail"];
    //read the mailbox
    if (count($voicemailArr)>0) {
      $voicemailOn="1";
      $email=$voicemailArr["email"];
    } else $voicemailOn="0";
    //inizialize DND
    $ret=getFromDB("DND",$cwUserInfo["exten"]);
    if($ret["present"]==1) $DND=1; else $DND=0;
    //inizialize CFU
    $ret=getFromDB("CFU",$cwUserInfo["exten"]);
    if($ret["present"]==1) {$CFU=1; $CFU_target=$ret["value"]; $CFU_disabled="";}
    else {$CFU=0; $CFU_disabled="disabled=\"disabled\"";}
    //inizialize CFB
    $ret=getFromDB("CFB",$cwUserInfo["exten"]);
    if($ret["present"]==1) {$CFB=1; $CFB_target=$ret["value"]; $CFB_disabled="";}
    else {$CFB=0; $CFB_disabled="disabled=\"disabled\"";}
    //inizialize CFNR
    $ret=getFromDB("CFNR",$cwUserInfo["exten"]);
    if($ret["present"]==1) {$CFNR=1; $CFNR_target=$ret["value"]; $CFNR_disabled="";}
    else {$CFNR=0; $CFNR_disabled="disabled=\"disabled\"";}
    //title
    $title=sprintf(T_("Account: %1\$s &lt;%2\$s %3\$s&gt;"),$extension ,$surname, $name );
  }
  else die;
  //create the language list
  $availLang=getAvailLang();
  foreach($availLang as $l) {
    if ($l==$language or (!isset($language) and $l==$cwLanguage)) $s="selected=\"selected\""; else $s="";
    $langSelect.="<option value=\"$l\" $s>$l</option>\n";
  }

  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <div class="SectionHeader">
  <?=$alert?>
    <h1><?=$title?></h1>
  </div>

  <div id="basic_form">
    <form action="<?=$PHP_SELF?><?if ($_GET["id"]!="") echo "?id=".$_GET["id"]; ?>" method="post">
<? if ($cwUserInfo["entity_id"]!=0) { ?>
      <table>
        <tr>
          <td>
            <p class="blockintro"><?=T_("Personal information")?></p>
            <div class="block">
              <input type="hidden" id="client[id]" name="client[id]" value="<?=$id?>" />
              <input type="hidden" id="client[tech]" name="client[tech]" value="<?=$technology?>" />
              <dl>
                <dt class="required"><label for="client[name]"><?=T_("Name")?>:</label></dt>
                  <dd><input disabled="disabled" type="text" size="25" maxlength="30" id="name" name="name" value="<?=$name?>" /></dd>
                <dt class="required"><label for="client[surname]"><?=T_("Surname")?>:</label></dt>
                  <dd><input disabled="disabled" type="text" size="25" maxlength="50" id="surname" name="surname" value="<?=$surname?>" /></dd>
                <dt class="required"><label for="client[extension]"><?=T_("Extension")?>:</label></dt>
                  <dd><input disabled="disabled" type="text" size="25" maxlength="20" id="extension" name="extension" value="<?=$extension?>" /></dd>
                <dt class="required"><label for="client[alias]"><?=T_("Alias")?>:</label></dt>
                  <dd><input disabled="disabled" type="text" size="25" maxlength="100" id="alias" name="alias" value="<?=$alias?>" /></dd>
                <dt class="required"><label for="client[language]"><?=T_("Language")?>:</label></dt>
                  <dd><select id="client[language]" name="client[language]"><?=$langSelect?></select></dd>
              </dl>
            </div>
          </td>
          <td>
            <p class="blockintro"><?=T_("Account Information")?></p>
            <div class="block">
              <dl>
                <dt class="required"><label for="client[username]"><?=T_("Username")?>:</label></dt>
                  <dd><input disabled="disabled" type="text" size="25" maxlength="100" id="client[username]" name="client[username]" value="<?=$username?>" />
                <dt class="required"><label for="client[oldpassword]"><?=T_("Password")?>:</label></dt>
                  <dd><input type="password" size="25" maxlength="20" id="client[oldpassword]" name="client[oldpassword]" value="" disabled="disabled" /></dd>
                <dt class="required"><label for="client[changepwd]"></label></dt>
                  <dd><input type="checkbox" id="client[changepwd]" name="client[changepwd]" onclick="if(this.checked){document.getElementById('client[oldpassword]').disabled=''; document.getElementById('client[newpassword]').disabled=''; document.getElementById('client[confirmpassword]').disabled=''; } else {document.getElementById('client[oldpassword]').disabled='disabled'; document.getElementById('client[newpassword]').disabled='disabled'; document.getElementById('client[confirmpassword]').disabled='disabled'; document.getElementById('client[oldpassword]').value=''; document.getElementById('client[newpassword]').value=''; document.getElementById('client[confirmpassword]').value=''; }"/>
                      <?=T_("Change password")?></dd>
                <dt class="required"><label for="client[newpassword]"><?=T_("New Password")?>:</label></dt>
                  <dd><input type="password" size="25" maxlength="20" id="client[newpassword]" name="client[newpassword]" value="" disabled="disabled" /></dd>
                <dt class="required"><label for="client[confirmpassword]"><?=T_("Confirm")?>:</label></dt>
                  <dd><input type="password" size="25" maxlength="20" id="client[confirmpassword]" name="client[confirmpassword]" value="" disabled="disabled" /></dd>
              </dl>
            </div>
          </td>
        </tr>
      </table>
      <!-- VoiceMail -->
      <? if($voicemailOn==1){ ?>
      	<h2><?=T_("VoiceMail")?></h2>
        <p class="blockintro"><?=T_("Insert here the voicemail informations")?></p>
        <div class="block">
          <dl>
            <dt class="required"><label for="client[vemail]"><?=T_("Email")?>:</label></dt>
              <dd><input type="text" size="25" maxlength="100" id="client[vemail]" name="client[vemail]" value="<?=$email?>" /> <?=T_("the email where it will be sent the voicemail")?></dd>
            <dt class="required"><label for="client[voldpassword]"><?=T_("Password")?>:</label></dt>
              <dd><input type="password" size="25" maxlength="20" id="client[voldpassword]" name="client[voldpassword]" value="" disabled="disabled"/> <?=T_("the password to read the voicemail")?></dd>
            <dt class="required"><label for="client[vchangepwd]"></label></dt>
              <dd><input type="checkbox" id="client[vchangepwd]" name="client[vchangepwd]" onclick="if(this.checked){document.getElementById('client[voldpassword]').disabled=''; document.getElementById('client[vnewpassword]').disabled=''; document.getElementById('client[vconfirmpassword]').disabled=''; } else {document.getElementById('client[voldpassword]').disabled='disabled'; document.getElementById('client[vnewpassword]').disabled='disabled'; document.getElementById('client[vconfirmpassword]').disabled='disabled'; document.getElementById('client[voldpassword]').value=''; document.getElementById('client[vnewpassword]').value=''; document.getElementById('client[vconfirmpassword]').value=''; }"/>
                  <?=T_("Change password")?></dd>
            <dt class="required"><label for="client[vnewpassword]"><?=T_("New Password")?>:</label></dt>
              <dd><input type="password" size="25" maxlength="20" id="client[vnewpassword]" name="client[vnewpassword]" value="" disabled="disabled" /> <?=T_("set the new password (it must be numeric, i.e. <strong>2525</strong>)")?></dd>
            <dt class="required"><label for="client[vconfirmpassword]"><?=T_("Confirm")?>:</label></dt>
              <dd><input type="password" size="25" maxlength="20" id="client[vconfirmpassword]" name="client[vconfirmpassword]" value="" disabled="disabled" /> <?=T_("retype the new password")?></dd>
          </dl>
        </div>
      <? } ?>
      <!-- DND -->
      <h2><?=T_("DND (Do Not Disturb)")?></h2>
      <p class="blockintro"><?=T_("Ignore any incoming calls")?></p>
      <div class="block">
        <dl>
          <dt class="required"><label for="DND[status]">DND:</label></dt>
          <dd>
            <input type="radio" id="DND[status]" name="DND[status]" value="1" <?if ($DND==1) echo "checked=\"checked\""; ?> /> <?=T_("ON")?>
            <input type="radio" id="DND[status]" name="DND[status]" value="0" <?if ($DND!=1) echo "checked=\"checked\""; ?> /> <?=T_("OFF")?>
          </dd>
        </dl>
      </div>
      <!-- CF -->
      <h2><?=T_("Call Forwarding")?></h2>
      <p class="blockintro"><?=T_("Route calls to some other destination")?></p>
      <div class="block">
        <dl>
          <table>
            <tr>
              <td>
                <dt class="required"><label for="CFU[status]">CFU:</label></dt>
                <dd>
                  <input type="radio" id="CFU[status]" name="CFU[status]" value="1" onclick="document.getElementById('CFU[target]').disabled='';" <?if ($CFU==1) echo "checked=\"checked\""; ?> /> <?=T_("ON")?>
                  <input type="radio" id="CFU[status]" name="CFU[status]" value="0" onclick="document.getElementById('CFU[target]').disabled='disabled';" <?if ($CFU!=1) echo "checked=\"checked\""; ?> /> <?=T_("OFF")?>
                </dd>
              </td>
              <td><?=T_("(Call Forward Unconditional) calls are forwarded to number")?>:</td>
              <td><input type="text"  id="CFU[target]" name="CFU[target]" value="<?=$CFU_target?>" <?=$CFU_disabled?> /></td>
            </tr>
            <tr>
              <td>
                <dt class="required"><label for="CFB[status]"><?=T_("CFB")?>:</label></dt>
                <dd>
                  <input type="radio" id="CFB[status]" name="CFB[status]" value="1" onclick="document.getElementById('CFB[target]').disabled='';" <?if ($CFB==1) echo "checked=\"checked\""; ?> /> <?=T_("ON")?>
                  <input type="radio" id="CFB[status]" name="CFB[status]" value="0" onclick="document.getElementById('CFB[target]').disabled='disabled';" <?if ($CFB!=1) echo "checked=\"checked\""; ?> /> <?=T_("OFF")?>
                </dd>
              </td>
              <td><?=T_("(Call forward on Busy) calls are forwarded to number")?>:</td>
              <td><input type="text"  id="CFB[target]" name="CFB[target]" value="<?=$CFB_target?>" <?=$CFB_disabled?> /></td>
            </tr>
            <tr>
              <td>
                <dt class="required"><label for="CFNR[status]"><?=T_("CFNR")?>:</label></dt>
                <dd>
                  <input type="radio" id="CFNR[status]" name="CFNR[status]" value="1" onclick="document.getElementById('CFNR[target]').disabled='';"<?if ($CFNR==1) echo "checked=\"checked\""; ?> /> <?=T_("ON")?>
                  <input type="radio" id="CFNR[status]" name="CFNR[status]" value="0" onclick="document.getElementById('CFNR[target]').disabled='disabled';"<?if ($CFNR!=1) echo "checked=\"checked\""; ?> /> <?=T_("OFF")?>
                </dd>
              </td>
              <td><?=T_("(Call forward on No Reply) calls are forwarded to number")?>:</td>
              <td><input type="text" id="CFNR[target]" name="CFNR[target]" value="<?=$CFNR_target?>" <?=$CFNR_disabled?> /></td>
            </tr>
          </table>
        </dl>
      </div>
      <!-- Action -->
      <div class="action">
        <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-save.gif" alt="Save" onclick="return check();" />
      </div>
<? } ?>
    </form>
  </div>

  <script>
  function check(){
    var box=document.getElementById('client[vchangepwd]');
    if(box.checked==true){
      var pwd=document.getElementById('client[vnewpassword]').value;
      var ch=null;
      for(var i=0; i<pwd.length; i++) {
        ch = pwd.charCodeAt(i);
        if(!((ch>47)&&(ch<58))) {
          alert('<?=T_("Voicemail password must be numeric! Please change it.")?>');
          return false;
        }
      }
    }
    return true;
  }
  </script>
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start();
  ?>
        <div class="Sidebar">
          <h1><?=T_("Account Information")?></h1>
          <p><?=T_("Here is the account informations you can change")?></p>
        </div>
  <?
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================

  $smarty->assign("section", "account");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('main.tpl');
?>
